<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>business_add</title>
    <link rel="stylesheet" href="../../static/layer/css/layui.css" media="all"/>
    <script type="text/javascript" charset="utf-8" src="../../static/uediter/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="../../static/uediter/ueditor.all.min.js"></script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="../../static/uediter/lang/zh-cn/zh-cn.js"></script>
    <style>
        .view-form .layui-input-block {
            line-height: 36px;
            background: #eeeeee;
            padding-left: 20px;
            font-size: 16px;
        }
    </style>
</head>

<body>
<div style="padding: 20px;">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">选择模板：</label>
            <div class="layui-input-block">
                <select name="TEMPLATE_NAME" lay-filter="template">
                    <option value="">--请选择--</option>

                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">模板备注：</label>
            <div class="layui-input-block">
                <textarea name="TEMPLATE_REMARK" readonly class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">模板信息：</label>
            <div class="layui-input-block">
                <textarea id="editor" name="TEMPLATE_INFO" type="text/plain"
                          style="width:100%;height:500px;"></textarea>
            </div>
        </div>
        <div class="layui-form-item operate-button-group">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="print">打印</button>
                <a class="layui-btn" onclick="operate.cancel(this);">取消</a>
            </div>
        </div>
    </form>

</div>

<script type="text/javascript" src="../../static/js/common/queryUtils.js"></script>
<script type="text/javascript" src="../../static/js/common/typeUtils.js"></script>
<script type="text/javascript" src="../../static/layer/layui.js"></script>
<script type="text/javascript">

    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    var ue = UE.getEditor('editor', {
        toolbars: [
            ['fullscreen', 'print', 'source', 'undo', 'redo'],
            ['bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'superscript', 'subscript', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', 'cleardoc']
        ]
    });

</script>
<script type="text/javascript">
    layui.use(['jquery', 'form', 'layer', 'upload', 'element'], function () {
        var form = layui.form,
            layer = layui.layer,
            element = layui.element,
            $ = layui.jquery;


        // 正则实现replaceAll
        String.prototype.replaceAll = function(s1,s2){
            return this.replace(new RegExp(s1,"gm"),s2);
        };

        var id = QueryUtils.GetQueryString("id");

        // 模板下拉框
        form.on('select(template)', function (data) {
            if(data.value==""){
                $("textarea[name='TEMPLATE_REMARK']").val("");
                UE.getEditor('editor').setContent("", false);
            }
            $.ajax({
                url: "/template/info",
                data: {ID: data.value},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    debugger
                    if (res.errcode === "0") {
                        $("textarea[name='TEMPLATE_REMARK']").val(res.data.TEMPLATE_REMARK);
                        init.template2contract(res.data.TEMPLATE_INFO, id);
                    } else if (res.errcode === "1") {
                        layer.msg(res.errmsg, function () {
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });
        });

        //监听提交
        form.on('submit(print)', function (data) {
//            var info = UE.getEditor('editor').getContent();
            UE.getEditor('editor').execCommand( 'print' );
            /*$.ajax({
                url: "/servlet/PrintServlet",
                data: {INFO: info},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.errcode === "0") {
                        window.location.href = 'http://' + window.location.host + '/' + res.data.fileUrl;
                    } else if (res.errcode === "1") {
                        layer.msg(res.errmsg, function () {
                        });
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });*/
            return false;
        });

        var init = {
            // 获取所有模板名称和ID
            initTemplateSelect: function () {
                $.ajax({
                    url: "/template/list",
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        debugger
                        if (res.errcode === "0") {
                            if (!!res.data) {
                                for (var i = 0; i < res.data.length; i++) {
                                    var str = '<option value="' + res.data[i].ID + '">' + res.data[i].TEMPLATE_NAME + '</option>';
                                    $("select[name='TEMPLATE_NAME']").append(str);
                                    /*$("textarea[name='TEMPLATE_REMARK']").val(res.data[i].TEMPLATE_REMARK);
                                    init.template2contract(res.data[i].TEMPLATE_INFO, id);*/
                                }
                                form.render();
                            }
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            },
            // 通过合同ID获取合同详情
            initContractInfo: function (id) {
                var info = '';
                $.ajax({
                    url: "/LoanController/InCompliance",
                    data: {ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            info = res.data;
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                return info;
            },
            // 通过账单ID获取账单详情
            initBillInfo: function (id) {
                var info = '';
                $.ajax({
                    url: "/billController/listBill",
                    data: {ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            info = res.data;
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                return info;
            },
            // 将模板转换成合同
            template2contract: function (templateInfo, contractId) {
                var contractInfo = init.initContractInfo(contractId);
//                console.log(contractInfo);
//                console.log(templateInfo);


          /*  <p>创建时间：{{CREATE_DATE}}</p>
                <p>提交人：{{SUBMIT_PERSON_NAME}}</p>
                <p>租户：{{PERSON_NAME}}</p>
                <p>借款金额：{{LOAN_PRICE}}</p>
                <p>借款利率：{{BORROW_INTEREST_RATE}}</p>
                <p>借款期限：{{BORROW_PERIOD}}</p>
                <p>合同编号：{{CONTRACT_NUM}}</p>
                <p>合同签订日期：{{CON_DATE}}</p>
                <p>合同开始日期：{{CON_HR_DATE}}</p>
                <p>合同结束日期：{{CON_END_DATE}}</p>
                <p>滞纳金比率（元/）：{{LATE_PAYMENT_RATE}}</p>
                <p>备注：{{INFO}}</p>
                <p>签订人：{{SIGNER}}</p>
                <p>签订人电话：{{SIGNER_TEL}}</p>
                */



                templateInfo = templateInfo.replaceAll("{{CREATE_DATE}}", contractInfo.CREATE_DATE);                // 创建时间
                templateInfo = templateInfo.replaceAll("{{SUBMIT_PERSON_NAME}}", contractInfo.SUBMIT_PERSON_NAME);                 // 提交人
                templateInfo = templateInfo.replaceAll("{{PERSON_NAME}}", contractInfo.PERSON_NAME);        // 租户
                templateInfo = templateInfo.replaceAll("{{LOAN_PRICE}}", contractInfo.LOAN_PRICE);    // 借款金额
                templateInfo = templateInfo.replaceAll("{{BORROW_INTEREST_RATE}}", contractInfo.BORROW_INTEREST_RATE);                            // 借款利率
                templateInfo = templateInfo.replaceAll("{{BORROW_PERIOD}}", contractInfo.BORROW_PERIOD);                    // 借款期限
                templateInfo = templateInfo.replaceAll("{{CONTRACT_NUM}}", contractInfo.CONTRACT_NUM);                        // 合同编号
                templateInfo = templateInfo.replaceAll("{{CON_DATE}}", contractInfo.CON_DATE);                  // 合同签订日期
                templateInfo = templateInfo.replaceAll("{{CON_HR_DATE}}", contractInfo.CON_HR_DATE);                                // 合同开始日期
                templateInfo = templateInfo.replaceAll("{{CON_END_DATE}}", contractInfo.CON_END_DATE);                   // 合同结束日期
                templateInfo = templateInfo.replaceAll("{{LATE_PAYMENT_RATE}}", contractInfo.LATE_PAYMENT_RATE);   // 滞纳金比率（元/）
                templateInfo = templateInfo.replaceAll("{{INFO}}",contractInfo.INFO);    // 备注
                templateInfo = templateInfo.replaceAll("{{SIGNER}}", contractInfo.SIGNER);                                // 签订人
                templateInfo = templateInfo.replaceAll("{{BUSS_CODE}}", contractInfo.BUSS_CODE);  //统一社会信用码
                templateInfo = templateInfo.replaceAll("{{INDUSTRY_NAME}}", contractInfo.INDUSTRY_NAME);//行业类别
                templateInfo = templateInfo.replaceAll("{{COMPANY_TYPE}}", contractInfo.COMPANY_TYPE);  //公司类型
                templateInfo = templateInfo.replaceAll("{{REG_CAPITAL}}", contractInfo.REG_CAPITAL);  //注册资金 认缴
                templateInfo = templateInfo.replaceAll("{{REG_CAPITAL_PAY}}", contractInfo.REG_CAPITAL_PAY);  //注册资金 认缴
                templateInfo = templateInfo.replaceAll("{{REG_ADDRESS}}", contractInfo.REG_ADDRESS);  //注册地址
                templateInfo = templateInfo.replaceAll("{{BUSINESS_ADDRESS}}", contractInfo.BUSINESS_ADDRESS);  //经营地址
                templateInfo = templateInfo.replaceAll("{{LEGAL_PERSON_NAME}}", contractInfo.LEGAL_PERSON_NAME);  //法人
                templateInfo = templateInfo.replaceAll("{{LEGAL_PERSON_TEL}}", contractInfo.LEGAL_PERSON_TEL);  //法人电话
                //公司信息
                templateInfo = templateInfo.replaceAll("{{HYYJ}}", contractInfo.HYYJ);  //行业预警

                templateInfo = templateInfo.replaceAll("{{SIGNER_TEL}}", contractInfo.SIGNER_TEL);        // 签订人电话
             /*   var con_date = contractInfo.contract.CON_DATE;
                var start_date = contractInfo.contract.CON_HR_DATE;
                var end_date = contractInfo.contract.CON_END_DATE;
                var CON_YEAR = con_date.substring(0, 4);
                var CON_MONTH = con_date.substring(5, 7);
                var CON_DAY = con_date.substring(8, 10);
                var START_YEAR = start_date.substring(0, 4);
                var START_MONTH = start_date.substring(5, 7);
                var START_DAY = start_date.substring(8, 10);
                var END_YEAR = end_date.substring(0, 4);
                var END_MONTH = end_date.substring(5, 7);
                var END_DAY = end_date.substring(8, 10);
                templateInfo = templateInfo.replaceAll("{{CON_YEAR}}", CON_YEAR);                 // 签订日期-年
                templateInfo = templateInfo.replaceAll("{{CON_MONTH}}", CON_MONTH);               // 签订日期-月
                templateInfo = templateInfo.replaceAll("{{CON_DAY}}", CON_DAY);                   // 签订日期-日
                templateInfo = templateInfo.replaceAll("{{START_YEAR}}", START_YEAR);             // 计租日期-年
                templateInfo = templateInfo.replaceAll("{{START_MONTH}}", START_MONTH);           // 计租日期-月
                templateInfo = templateInfo.replaceAll("{{START_DAY}}", START_DAY);               // 计租日期-日
                templateInfo = templateInfo.replaceAll("{{END_YEAR}}", END_YEAR);                 // 结束日期-年
                templateInfo = templateInfo.replaceAll("{{END_MONTH}}", END_MONTH);               // 结束日期-月
                templateInfo = templateInfo.replaceAll("{{END_DAY}}", END_DAY);                   // 结束日期-日*/

                UE.getEditor('editor').setContent(templateInfo, false);
            },

            // 金额转大写
            price2dx: function (price) {
                return TypeUtils.NumberToMoney(price);
            },
            // 格式化日期-补零
            padDate: function (value) {
                return value < 10 ? '0' + value : value;
            }
        };

        init.initTemplateSelect();

    });

    var operate = {
        // 取消
        cancel: function (obj) {
            var i = parent.layer.getFrameIndex(window.name);
            parent.layer.close(i); // 关闭合同详情提示框
        }
    };
</script>
</body>

</html>